package com.dragon.manage.action;

import com.alibaba.fastjson.JSONObject;
import com.dragon.manage.bean.ManageUser;
import com.dragon.manage.service.IManageActiveService;
import com.dragon.manage.service.IManageService;
import com.dragon.manage.util.LogUtil;
import com.dragon.manage.util.ManageUtil;
import com.dragon.manage.util.RightUtil;
import com.dragon.manage.util.StaticData;
import com.dragon.manage.util.StringUtil;
import com.opensymphony.xwork2.ActionContext;

@SuppressWarnings("rawtypes")
public class ManageActiveAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private IManageService manageService;

	private IManageActiveService manageActiveService;
	
	public void setManageService(IManageService manageService) {
		this.manageService = manageService;
	}

	public void setManageActiveService(IManageActiveService manageActiveService) {
		this.manageActiveService = manageActiveService;
	}
	
	/**
	 * 获取活动列表
	 * @return
	 */
	public String getActiveList(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			String page=request.getParameter("page");
			if(page==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			result = manageActiveService.getActiveList(StringUtil.getParam(page));
			
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("获取活动列表", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
	
	/**
	 * 获取活动列表
	 * @return
	 */
	public String getActiveInfo(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			String activeId=request.getParameter("activeId");
			if(activeId==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			result = manageActiveService.getActiveInfo(StringUtil.getParam(activeId));
			
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("获取活动列表", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
	/**
	 * 新建一个活动
	 * @return
	 */
	public String createActivityInfo(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			//标题
			String activityTitle=request.getParameter("activityTitle");
			//列表图片
			String listImage=request.getParameter("listImage");
			/** 标题图片显示地址 */
			String activityImage=request.getParameter("activityImage");
			/** 开赛时间 */
			String startDate=request.getParameter("startDate");
			/** 封盘时间 */
			String endDate=request.getParameter("endDate");
			/** 所属于哪一类比赛 */
			String gameType=request.getParameter("gameType");
			/** 领奖截止时间（） */
			String overDate=request.getParameter("overDate");
			/** 下架时间（不显示） */
			String unshelveDate=request.getParameter("unshelveDate");
			/** 活动说明 */
			String activityRemark=request.getParameter("activityRemark");
			/** 分享的标题 */
			String fenxiangTitle=request.getParameter("fenxiangTitle");
			/** 分享的图片 */
			String fenxiangImage=request.getParameter("fenxiangImage");
			/** 分享的内容 */
			String fenxiangContent=request.getParameter("fenxiangContent");
			/** 需要的金币数量 */
			String needGold=request.getParameter("needGold");
			/** 最大参与次数 -1为无限制 */
			String maxTimes=request.getParameter("maxTimes");
			/** 需要邀请用户 */
			String needUser=request.getParameter("needUser");
			/** 奖品名称 */
			String award=request.getParameter("award");
			/** banner图片url */
			String activityImageUrl=request.getParameter("activityImageUrl");
			/** 页脚图片url */
			String footImageUrl=request.getParameter("footImageUrl");
			/** 页脚图片 */
			String footImage=request.getParameter("footImage");
			if(activityTitle==null||listImage==null||activityImage==null
					||startDate==null||endDate==null||gameType==null
					||overDate==null||unshelveDate==null||activityRemark==null
					||fenxiangTitle==null||fenxiangImage==null||fenxiangContent==null
					||needGold==null||maxTimes==null||needUser==null||award==null
					||maxTimes==null||needUser==null||award==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			
			result = manageActiveService.createActivityInfo(activityTitle, listImage,
					activityImage, startDate, endDate, StringUtil.getParam(gameType), overDate,
					unshelveDate, activityRemark, fenxiangTitle, fenxiangImage,fenxiangContent,
					 StringUtil.getParam(needGold),StringUtil.getParam(maxTimes),
					 StringUtil.getParam(needUser),award,activityImageUrl,footImageUrl,footImage);
			
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("新建一个活动", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}

	/**
	 * 更新活动
	 * @return
	 */
	public String updateActivityInfo(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			
			String id = request.getParameter("id");
			if(id==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			/**标题*/
			String activityTitle=request.getParameter("activityTitle");
			/**列表图片*/
			String listImage=request.getParameter("listImage");
			/** 标题图片显示地址 */
			String activityImage=request.getParameter("activityImage");
			/** 开赛时间 */
			String startDate=request.getParameter("startDate");
			/** 封盘时间 */
			String endDate=request.getParameter("endDate");
			/** 所属于哪一类比赛 */
			String gameType=request.getParameter("gameType");
			/** 领奖截止时间（） */
			String overDate=request.getParameter("overDate");
			/** 下架时间（不显示） */
			String unshelveDate=request.getParameter("unshelveDate");
			/** 活动说明 */
			String activityRemark=request.getParameter("activityRemark");
			/** 分享的标题 */
			String fenxiangTitle=request.getParameter("fenxiangTitle");
			/** 分享的图片 */
			String fenxiangImage=request.getParameter("fenxiangImage");
			/** 分享的内容 */
			String fenxiangContent=request.getParameter("fenxiangContent");
			/** 需要的金币数量 */
			String needGold=request.getParameter("needGold");
			/** 最大参与次数 -1为无限制 */
			String maxTimes=request.getParameter("maxTimes");
			/** 需要邀请用户 */
			String needUser=request.getParameter("needUser");
			/** 奖品名称 */
			String award=request.getParameter("award");
			/** banner图片url */
			String activityImageUrl=request.getParameter("activityImageUrl");
			/** 页脚图片url */
			String footImageUrl=request.getParameter("footImageUrl");
			/** 页脚图片 */
			String footImage=request.getParameter("footImage");
			result = manageActiveService.updateActivityInfo(activityTitle, listImage,
					activityImage, startDate, endDate, StringUtil.getParam(gameType), overDate,
					unshelveDate, activityRemark, fenxiangTitle, fenxiangImage,fenxiangContent,
					 StringUtil.getParam(needGold),StringUtil.getParam(maxTimes),
					 StringUtil.getParam(needUser),award,activityImageUrl,footImageUrl,footImage,
					 StringUtil.getParam(id));
			
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("更新活动", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}

	/**
	 * 创建活动选项
	 * @return
	 */
	public String createActivityOption(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			/** 活动ID */
			String activityId=request.getParameter("activityId");
			/** 标题 */
			String title=request.getParameter("title");
			/** 竞猜名称 */
			String amusementName=request.getParameter("amusementName");
			/** 下注项目信息 id#文字#赔率 */
			String oddsInfo=request.getParameter("oddsInfo");
			/** 下注选项类型（每行显示几个选项） */
			String oddsInfoType=request.getParameter("oddsInfoType");

			
			if(activityId==null||title==null||amusementName==null
					||oddsInfo==null||oddsInfoType==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			
			result = manageActiveService.createActivityOption(StringUtil.getParam(activityId), 
					title, amusementName, oddsInfo, StringUtil.getParam(oddsInfoType));
			
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("创建活动选项", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}

	/**
	 * 获取活动列表
	 * @return
	 */
	public String getActivityOptionList(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			String activeId=request.getParameter("activeId");
			if(activeId==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			result = manageActiveService.getActivityOptionList(StringUtil.getParam(activeId));
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("更新活动选项", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
	
	/**
	 * 更新活动选项
	 * @return
	 */
	public String updateActivityOption(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			String id=request.getParameter("id");
			if(id==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			/** 活动ID */
			String activityId=request.getParameter("activityId");
			/** 标题 */
			String title=request.getParameter("title");
			/** 竞猜名称 */
			String amusementName=request.getParameter("amusementName");
			/** 下注项目信息 id#文字#赔率 */
			String oddsInfo=request.getParameter("oddsInfo");
			/** 下注选项类型（每行显示几个选项） */
			String oddsInfoType=request.getParameter("oddsInfoType");
			/** 活动结果 */
			String amusementResult=request.getParameter("amusementResult");
			result = manageActiveService.updateActivityOption(StringUtil.getParam(activityId), 
					title, amusementName, oddsInfo, StringUtil.getParam(oddsInfoType),
					StringUtil.getParam(id),StringUtil.getParam(amusementResult));
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("更新活动选项", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
	
	/**
	 * 提交结果
	 */
	public String overActivityOption(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			String activityId=request.getParameter("activityId");
			if(activityId==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}

			result = manageActiveService.overActivityOption(StringUtil.getParam(activityId));
			
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("提交结果", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}


}
